﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Matchmaking.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The matchmaking.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Services.Dto
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;

    /// <summary>
    /// The matchmaking.
    /// </summary>
    [DataContract]
    public class Matchmaking
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the buyers.
        /// </summary>
        [DataMember]
        public string Buyers { get; set; }

        /// <summary>
        /// Gets or sets the comments.
        /// </summary>
        [DataMember]
        public string Comments { get; set; }

        /// <summary>
        /// Gets or sets the date.
        /// </summary>
        [DataMember]
        public DateTime? Date { get; set; }

        /// <summary>
        /// Gets or sets the funds.
        /// </summary>
        [DataMember]
        public List<Guid> Funds { get; set; }

        /// <summary>
        /// Gets or sets the funds text.
        /// </summary>
        [DataMember]
        public string FundsText { get; set; }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        [DataMember]
        public Guid ID { get; set; }

        /// <summary>
        /// Gets or sets the location.
        /// </summary>
        [DataMember]
        public Guid Location { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        [DataMember]
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the other financing.
        /// </summary>
        [DataMember]
        public List<Guid> OtherFinancing { get; set; }

        /// <summary>
        /// Gets or sets the owner projects.
        /// </summary>
        [DataMember]
        public List<Guid> OwnerProjects { get; set; }

        /// <summary>
        /// Gets or sets the owner profiles.
        /// </summary>
        [DataMember]
        public List<Guid> Profiles { get; set; }

        /// <summary>
        /// Gets or sets the partners.
        /// </summary>
        [DataMember]
        public List<Guid> Partners { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The convert.
        /// </summary>
        /// <param name="matchmaking">
        /// The matchmaking.
        /// </param>
        /// <returns>
        /// The <see cref="Matchmaking"/>.
        /// </returns>
        public static Matchmaking Convert(Models.Domain.Matchmaking matchmaking)
        {
            if (matchmaking != null)
            {
                var dto = new Matchmaking
                              {
                                  ID = matchmaking.ID, 
                                  Name = matchmaking.Name,
                                  Date = matchmaking.TimeOfActivity, 
                                  Location =
                                      matchmaking.Location != null ? matchmaking.Location.ID : Guid.Empty, 
                                  Buyers = matchmaking.Buyers, 
                                  Comments = matchmaking.Comments, 
                                  FundsText = matchmaking.FundsText,
                                  Funds = (from s in matchmaking.Funds select s.ID).ToList(),
                                  Partners = (from s in matchmaking.Partners select s.ID).ToList(),
                                  Profiles = (from s in matchmaking.Profiles select s.ID).ToList(), 
                                  OtherFinancing = (from s in matchmaking.OtherFinancing select s.ID).ToList()
                              };

                if (matchmaking.OwnerProjects != null && matchmaking.OwnerProjects.Any())
                {
                    dto.OwnerProjects = new List<Guid>();
                    foreach (Models.Domain.Project proj in matchmaking.OwnerProjects)
                    {
                        dto.OwnerProjects.Add(proj.ID);
                    }
                }

                return dto;
            }
            
            return null;
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Queries
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Activities;
    using ServiceLibrary.Services.Dto;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The matchmaking query.
    /// </summary>
    [DataContract]
    public class MatchmakingQuery : IQuery
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the default fields.
        /// </summary>
        [DataMember]
        public DefaultSearchFields DefaultFields { get; set; }

        /// <summary>
        /// Gets or sets the guid of the location.
        /// </summary>
        [DataMember]
        public Guid? AtLocation { get; set; }

        /// <summary>
        /// Gets or sets the guid of the partner type that should be included in the query.
        /// </summary>
        [DataMember]
        public Guid? WithPartnerType { get; set; }

        /// <summary>
        /// Gets or sets the list of fund ids that should be a part of the result.
        /// </summary>
        [DataMember]
        public List<Guid> ContainsFunds { get; set; }

        /// <summary>
        /// Gets or sets the list of profile ids that should be a part of the result.
        /// </summary>
        [DataMember]
        public List<Guid> Profiles { get; set; }

        /// <summary>
        /// Gets or sets whether the profiles are at remote a area.
        /// </summary>
        [DataMember]
        public bool? ProfilesAreAtRemoteArea { get; set; }

        /// <summary>
        /// Gets or sets if it's an entrepreneur.
        /// </summary>
        [DataMember]
        public bool? ProfilesAreEntrepreneur { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public override IResult Execute(ISession hsession)
        {
            var actioner = new MatchmakingActioner(hsession, null);

            return actioner.ExecuteSearchQuery(this);
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Results
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using ServiceLibrary.Services.Dto;

    /// <summary>
    /// The matchmaking result.
    /// </summary>
    [DataContract]
    public class MatchmakingResult : IResult
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MatchmakingResult"/> class.
        /// </summary>
        public MatchmakingResult()
        {
            this.List = new List<Matchmaking>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        [DataMember]
        public List<Matchmaking> List { get; set; }

        /// <summary>
        /// Gets or sets the total row count.
        /// </summary>
        [DataMember]
        public int TotalRowCount { get; set; }

        #endregion
    }
}

namespace ServiceLibrary.Services.Commands
{
    using System;
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Activities;
    using ServiceLibrary.Services.Dto;

    using User = ServiceLibrary.Models.Domain.User;

    /// <summary>
    /// The create matchmaking command.
    /// </summary>
    [DataContract]
    public class CreateMatchmakingCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the new matchmaking.
        /// </summary>
        [DataMember]
        public Matchmaking NewMatchmaking { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new MatchmakingActioner(hsession, activeUser);
            actioner.CreateMatchmaking(this);
        }

        #endregion
    }

    /// <summary>
    /// The update matchmaking command.
    /// </summary>
    [DataContract]
    public class UpdateMatchmakingCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Matchmaking Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new MatchmakingActioner(hsession, activeUser);
            actioner.UpdateMatchmaking(this);
        }

        #endregion
    }

    /// <summary>
    /// The delete matchmaking command.
    /// </summary>
    [DataContract]
    public class DeleteMatchmakingCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Guid? Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new MatchmakingActioner(hsession, activeUser);
            actioner.DeleteMatchmaking(this);
        }

        #endregion
    }
}